﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UseExample : MonoBehaviour
{
	List<string> infoall;
	public Text text;

    public InputField inputField;
    public Button add_button;
    public Button delete_button;

    void Start()
	{
        add_button.onClick.AddListener(() => 
        {
            AddContent(inputField.text); 
        });
        delete_button.onClick.AddListener(DeleteTxt);

        //读txt
        infoall = ReadWriteTxt.Instance.LoadFile(Application.persistentDataPath, "TestReadWrite.txt");
        if (infoall != null)    //如果存在
        {
            ShowStr(infoall);
        }
        else        //文件不存在
        {
            //创建txt
            ReadWriteTxt.Instance.CreateFile(Application.persistentDataPath, "TestReadWrite.txt", "这里是txt里的内容");
        }
        infoall = ReadWriteTxt.Instance.LoadFile(Application.persistentDataPath, "TestReadWrite.txt");
        ShowStr(infoall);
    }

    private void AddContent(string _str)
    {
        ReadWriteTxt.Instance.CreateFile(Application.persistentDataPath, "TestReadWrite.txt", "添加的内容：" + _str);
        infoall = ReadWriteTxt.Instance.LoadFile(Application.persistentDataPath, "TestReadWrite.txt");
        ShowStr(infoall);
    }

    private void DeleteTxt()
    {
        ReadWriteTxt.Instance.DeleteFile(Application.persistentDataPath, "TestReadWrite.txt");
    }

    private void ShowStr(List<string> _str)
    {
        string str = "";
        for (int i = 0; i < infoall.Count; i++)
        {
            str += infoall[i] + "\n";
        }
        text.text = str;
    }

}